#! /usr/bin/env python
# -*- coding: utf-8 -*-

import ez_setup
import shutil, sys, os, glob

ez_setup.use_setuptools()
 
from setuptools import setup, find_packages
import py2exe

curpath = os.path.dirname(os.path.abspath(__file__))
def find_py_modules():

    fileList = (os.path.split(full)[-1] for full in glob.glob(os.path.join(curpath, r'src/*.py')))
    return [os.path.splitext(fileName)[0] for fileName in fileList]

def find_data_files(base_dir, files=['logging.conf', 'urls.cfg', 'spider.conf']):
    data_files = []
    for fAbsPath in (os.path.join(curpath,base_dir ,f) for f in files):
        if os.path.exists(fAbsPath):
            data_files.append(fAbsPath)
    return data_files

def singleSetUp(site_name): 
    dist_dir = 'dist/%s' % site_name
    if os.path.isdir(dist_dir): # 删除上次的生成结果
        print 'rm %s dist_dir first' % dist_dir
        shutil.rmtree(dist_dir)
    bDir = 'src/%s' % site_name
    setup(
        name = "%sspider" % site_name,
        cmdclass = {'py2exe': py2exe.build_exe.py2exe},
        version = '1.0',
        packages = find_packages(bDir),# include all packages under src
        package_dir = {'':'src'}, # tell distutils packages are under src
        py_modules = find_py_modules(),
        console = ['src/%s/%sspider.py' % (site_name,site_name)],
        zip_safe=True,
        #test_suite = "test.test_enum.suite",
        package_data={'': ["*.*"],},
        options={'py2exe': {'optimize': 2,
                        'compressed': True,
                        'dist_dir': dist_dir, } },
    
        #data_files = find_data_files(bDir),
        # installed or upgraded on the target machine  
        install_requires=['chardet', 'enum', 'BeautifulSoup', 'threadpool'],
        # PyPI metadata
        # metadata for upload to PyPI
        author="zhongfeng",
        author_email="fzhong@travelsky.com",
        description="21obuys Package",
        license="PSF",
        keywords="crawlers",
        classifiers=[
            "Development Status :: 4 - Beta",
            "License :: OSI Approved :: GNU General Public License (GPL)",
            "License :: OSI Approved :: Python Software Foundation License",
            "Programming Language :: Python",
            "Topic :: Software Development :: Libraries :: Python Modules",
            "Operating System :: OS Independent",
            "Intended Audience :: Developers",
            ],
 )

class BuildSpiderExe(object):
    def __init__(self):
        self.sites=['amazon','coo8','dangdang','gome','icson','j360buy',
                    'newegg','suning','efeihu','lusen']
        #self.sites = ['j360buy']
    def run(self):
        for site_name in self.sites:
            singleSetUp(site_name)
        if os.path.isdir('build'): # 清除build文件夹
            shutil.rmtree('build')
 
if __name__ == '__main__':
    if len(sys.argv) < 2:
        sys.argv.append('py2exe')
    BuildSpiderExe().run()

